<template>
  <div>
    <BreakSpace label="编辑下发调查要求" :containerStyle="{ marginBottom: '15px' }" />
    <FormPanel
      ref="formPanel"
      :list="formList"
      labelWidth="100"
      :formType="type"
      :isSubmitBtn="false"
      @formChange="formChangeHandle"
    />
    <div
      :style="{
        textAlign: 'center'
      }"
    >
      <el-button @click="saveDialog(1)" type="primary" :loading="saveLoading" v-if="type==='edit'">下 发</el-button>
      <el-button @click="saveDialog(2)" type="primary" :loading="saveLoading" v-if="type==='edit'">驳回</el-button>
      <el-button @click="closeDialog">返 回</el-button>
    </div>
  </div>
</template>

<script>
  import { dictionary } from '@/utils/dictMixin';
  import { getFileGroup } from '@/api/partsmanage/common/file';
  import moment from 'moment';
  import { phone } from '@/utils/validate';
  import {updateCustomerService} from '@/api/customerService/secneExamine/secneLssue/index';
  export default {
    name: "updateSecneLssue",
    mixins: [dictionary],
    props: {
      type: {
        type: String,
        default: 'add',
      },
      rowData: {
        type: Object,
        default: () => ({})
      }
    },
    data() {
      this.saveData = null;
      return {
        formList: this.createFormList(),
        fetchApi: null,
        fetchParams: null,
        saveLoading: false,
      };
    },
    created() {
      this.init();
    },
    methods: {
      selectChange(val) {
        console.log('val', val);
        this.rowSelectData = val;
      },
      init() {
        this.rowData.endDate= moment(this.rowData.endDate).format('YYYY-MM-DD HH:mm:ss')
        this.rowData.beginDate= moment(this.rowData.beginDate).format('YYYY-MM-DD HH:mm:ss')
        this.rowData.applyDate= moment(this.rowData.applyDate).format('YYYY-MM-DD HH:mm:ss')
          console.info(this.type)
          console.info(this.rowData)
          this.formList.forEach(x => {
            x.initialValue = this.rowData[x.fieldName];
          });
        getFileGroup({ "xtdjid": this.rowData.id, "xtfjlx": "3" }).then(response => {
          console.info(response)
          var fileList = response.data.records
          this.formList.forEach(x => {
            if (x.fieldName === 'wayFiles') {
              x.initialValue = fileList
            }
          })
        })
      },
      createFormList() {
        return [
          {
            type: 'INPUT',
            label: '留车单号',
            fieldName: 'retainNo',
            disabled: true,
          },
          {
            type: 'INPUT',
            label: '车型',
            fieldName: 'model',
            disabled: true,
          },
          {
            type: 'SELECT',
            label: '状态',
            fieldName: 'state',
            itemList:this.createDictList('5008'),
            initialValue: '',
            disabled: true,
          },
          {
            type: 'INPUT',
            label: '配件代码',
            fieldName: 'partCode',
            disabled: true,
          },
          {
            type: 'INPUT',
            label: '配件名称',
            fieldName: 'partName',
            disabled: true,
          },
          {
            type: 'INPUT',
            label: '申请人',
            fieldName: 'applicant',
            disabled: true,
          },
          {
            type: 'DATE',
            label: '申请时间',
            fieldName: 'applyDate',
            format: 'yyyy-MM-dd HH:mm:ss',
            disabled: true,
          },
          {
            type: 'INPUT',
            label: '调查范围',
            fieldName: 'scope',
            disabled: true,
          },
          {
            type: 'INPUT',
            label: 'VIN区间',
            fieldName: 'vinBetween',
            disabled: true,
          },
          {
            type: 'DATE',
            label: '生产开始时间',
            fieldName: 'beginDate',
            format: 'yyyy-MM-dd HH:mm:ss',
            disabled: true,
          },
          {
            type: 'DATE',
            label: '生产结束时间',
            fieldName: 'endDate',
            format: 'yyyy-MM-dd HH:mm:ss',
            disabled: true,
          },
          {
            type: 'INPUT',
            label: '联系人',
            fieldName: 'liaison',
            rules: [{ required: true, message: '请输入联系人', trigger: 'blur' }]
          },
          {
            type: 'INPUT',
            label: '联系电话',
            fieldName: 'phone',
            rules: [
              { required: true, message: '请输入联系电话', trigger: 'blur' },
              { message: '请确认联系电话格式',validator: phone, trigger: 'blur' }
            ]
          },
          {
            type: 'INPUT',
            label: '故障现象',
            fieldName: 'faultPhenomenon',
            selfCols: 2,
            rules: [{ required: true, message: '请输入故障现象', trigger: 'blur' }]
          },
          {
            type: 'TEXT_AREA',
            selfCols: 3,
            label: '排查确认说明',
            fieldName: 'screening',
            maxlength: 200,
            disabled: true,
          },
          {
            type: 'UPLOAD_FILE',
            label: '附件',
            fieldName: 'wayFiles',
            placeholder: '附件',
            disabled: true,
            initialValue: '',
            upload: {
              actionUrl: '/api/file/oss/upload',
              limit: 5,
              fileSize: 20,
              tipText: '（支持：PDF、RAR、JPG格式）'
            }
          }
        ]
      },
      formChangeHandle(val) {
        this.saveData = val;
      },
      async saveDialog(type) {
        const err = this.$refs.formPanel.SUBMIT_FORM();
        if (err === null) {
          return false
        }
        this.saveLoading = true
        console.info(this.saveData)
        if (type === 1){
          this.saveData.state = 50081003
        }else{
          this.saveData.state = 50081002
        }
        const res = await updateCustomerService(this.rowData.id,this.saveData);
        if (res.resultCode === 200 ) {
          this.closeDialog('refresh');
        }
        this.saveLoading = false
      },
      closeDialog(param) {
        this.$emit('close', {visible: false, type: param});
      }
    }
  }
</script>

<style scoped>

</style>
